package Dao.Impl;

import Dao.EmpDao;
import bean.Emp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.CMUtility;
import util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class EmpDaoImpl implements EmpDao {
    Emp eemp = new Emp();

    @Override
    public List<Emp> getAllEmp() {
        List<Emp> query = null;
        QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
        try {
            query = queryRunner.query("select * from emp;", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return query;

        /**
         * 员工数据库实现类
         * @return
         */

    }

    @Override
    public void addEmp(Emp emp) {
        QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

        System.out.println("---------------------添加客户---------------------");
        try {
            queryRunner.insert("insert into emp values (?,?,?,?,?,?,?)", new ResultSetHandler<Object>() {
                @Override
                public Object handle(ResultSet resultSet) throws SQLException {
                    return null;
                }
            }, emp.getEmpno(), emp.getEname(), emp.getJob(), emp.getSal(), emp.getHiredate(), emp.getComm(), emp.getDeptno());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    Scanner scan = new Scanner(System.in);
    String ename;
    String job;
    double sal;
    Date hire;
    double comm;
    int deptno;

    @Override
    public void upDate(int empn) {

        QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

        try {
            List<Emp> empno = queryRunner.query("select * from emp",
                    new BeanListHandler<Emp>(Emp.class));
            // System.out.println(empno);

            for (int i = 0; i < empno.size(); i++) {
                if (empno.get(i).getEmpno() == empn) {
                    System.out.println("---------------------修改客户---------------------");
                    // System.out.println(p.getEmpno());
                    System.out.print("请修改员工姓名(" + empno.get(i).getEname() + "):");
                    //String ename1 = scan.nextLine();
                    ename = CMUtility.readString(10, empno.get(i).getEname());
                    System.out.print("请输入修改的工作(" + empno.get(i).getJob() + "):");
                    //String job1 = scan.nextLine();
                    job = CMUtility.readString(5, empno.get(i).getJob());
                    System.out.print("请修改员工薪水(" + empno.get(i).getSal() + "):");
                    // Double sal1 = scan.nextDouble();
                    sal = CMUtility.readDouble(empno.get(i).getSal());
                    System.out.print("请修改员工入职时间'(格式为：yyyy-MM-dd)：");
                    for (; ; ) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String dateStr = scan.next();

                        try {
                            hire = (Date) format.parse(dateStr);
                            break;
                        } catch (ParseException e) {
                            System.out.print("格式错误，请重新输入：");
                        }
                    }
                    System.out.print("请修改员工奖金(" + empno.get(i).getComm() + "):");
                    //Double comm1 = scan.nextDouble();
                    comm = CMUtility.readDouble(empno.get(i).getComm());
                    System.out.print("请输入员工部门(" + empno.get(i).getDeptno() + "):");
                    //deptno = scan.nextInt();
                    deptno = CMUtility.readInt(2, empno.get(i).getDeptno());
                    queryRunner.update("update emp set ename=?,job=?,sal=?,hiredate=?,comm=?," +
                                    "deptno=? where empno=?",
                            ename, job, sal, hire, comm, deptno, empn);
                    System.out.println("修改成功");
                } else if (i == empno.size()) {
                    System.out.println("此员工不存在！！！！！！！！");
                }

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public int delete(int empno) {
        int i = 0;
        try {
            QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
            i = qr.update("DELETE FROM emp WHERE empno=?", empno);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            return i;
        }
    }

    @Override
    public List<Emp> selectEmp(String name) {
        List<Emp> query = null;
        // Object[] prams={"%"+name+"%"};
        try {
            QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
            query = queryRunner.query("select * from emp where ename like concat('%',?,'%')",
                    new BeanListHandler<Emp>(Emp.class), name);
            if (query == null || query.size() == 0) {
                System.out.println("没有该员工！！！！！！！！！！！！！");
            }
            //System.out.println(query);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;

    }
}